package org.example;

import javax.swing.*;
import java.awt.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.awt.font.TextAttribute;

public class ResourceManager {
    private static ResourceManager instance;
    private Map<String, Image> images;
    private Map<String, Font> fonts;

    // 颜色常量
    public static final Color PRIMARY_TEXT = new Color(220, 240, 255);
    public static final Color CODE_TEXT = new Color(150, 250, 150);
    public static final Color CHALLENGE_TEXT = new Color(100, 200, 255);
    public static final Color ERROR_TEXT = new Color(255, 100, 100);
    public static final Color BACKGROUND_DARK = new Color(15, 20, 35);
    public static final Color BACKGROUND_MEDIUM = new Color(25, 30, 50);
    public static final Color BACKGROUND_LIGHT = new Color(40, 45, 70);

    // 新增UI颜色常量
    public static final Color UI_BLUE = new Color(70, 130, 180);
    public static final Color TEXT_WHITE = new Color(240, 240, 255);

    private ResourceManager() {
        images = new HashMap<>();
        fonts = new HashMap<>();
        loadResources();
    }

    public static synchronized ResourceManager getInstance() {
        if(instance == null) {
            instance = new ResourceManager();
        }
        return instance;
    }

    private void loadResources() {
        loadFonts(); // 调用loadFonts而不是重复代码
    }

    public Image getImage(String key) {
        return images.get(key);
    }

    public Font getFont(String key, float size) {
        Font font = fonts.get(key);
        return font != null ? font.deriveFont(size) : new Font("Arial", Font.PLAIN, (int)size);
    }

    private void loadFonts() {
        try {
            // 主标题字体 (更醒目的科技字体)
            Font titleFont = new Font("Microsoft YaHei", Font.BOLD, 72)
                    .deriveFont(Collections.singletonMap(TextAttribute.WEIGHT, TextAttribute.WEIGHT_EXTRABOLD));
            fonts.put("title", titleFont);

            // 正文字体 (提高可读性)
            Font mainFont = new Font("Microsoft YaHei", Font.PLAIN, 18);
            fonts.put("main", mainFont);

            // 代码字体 (等宽且清晰)
            Font codeFont = new Font("Consolas", Font.PLAIN, 16);
            fonts.put("code", codeFont);
        } catch (Exception e) {
            // 备用字体
            fonts.put("title", new Font("Arial", Font.BOLD, 72));
            fonts.put("main", new Font("Arial", Font.PLAIN, 16));
            fonts.put("code", new Font("Courier New", Font.PLAIN, 16));
        }
    }
}
